package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Slf4j
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    WmNewsService wmNewsService;

    /**
     * 查询频道列表
     * @return
     */
    @Override
    public ResponseResult findAll() {
        List<WmChannel> list = list();
        return ResponseResult.okResult(list);
    }

    /**
     * 条件分页查询频道列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getChannelPage(ChannelDto dto) {
        //1.检查参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();
        //2.分页
        IPage page = new Page(dto.getPage(), dto.getSize());
        //3.条件匹配
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper();
        //频道名称
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.like(WmChannel::getName,dto.getName());
        }
        //升序排序
        wrapper.orderByAsc(WmChannel::getOrd);
        //4.查询 返回结果
        page = page(page, wrapper);
        return new PageResponseResult(page);
    }

    /**
     * 新增频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult insert(WmChannel wmChannel) {
        //检查参数
        if (wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (StringUtils.isBlank(wmChannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_NAME_ISNULL);
        }
        //检查频道名称是否重复
        WmChannel channel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
        if (channel != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_NAME_DATA_EXIST);
        }
        //保存
        boolean save = save(wmChannel);

        return save ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS)
                    : ResponseResult.errorResult(AppHttpCodeEnum.FAIL);
    }

    /**
     * 修改频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult edit(WmChannel wmChannel) {
        //检查参数
        if (wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (StringUtils.isBlank(wmChannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_NAME_ISNULL);
        }
        //检查频道名称是否为重复
        WmChannel channel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
        if (channel != null && channel.getId() != wmChannel.getId()){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_NAME_DATA_EXIST);
        }
        //若状态要修改为禁用
        if (!wmChannel.getStatus()){
            //检查频道是否被引用
            List<WmNews> wmNewsList = wmNewsService.list(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, wmChannel.getId()));
            if (wmNewsList.size() > 0) {  //被引用则不允许禁用
                return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_DISABLE_FAIL);
            }
        }

        //更新
        boolean update = updateById(wmChannel);
        return update ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS)
                : ResponseResult.errorResult(AppHttpCodeEnum.FAIL);
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        //检查频道状态
        WmChannel channel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getId, id));
        if (channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (channel.getStatus()){  //状态为启用则不允许删除
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_DEL_FAIL);
        }

        //删除
        boolean del = removeById(id);
        return del ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS)
                : ResponseResult.errorResult(AppHttpCodeEnum.FAIL);
    }
}
